import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import joblib  # 用于保存/加载模型

# 加载模型（你可以用 joblib.dump(best_model, 'model.pkl') 保存）
model = joblib.load('model.pkl')

# 页面标题
st.title("3小时降水量预测系统")

st.markdown("输入气象参数，预测未来3小时的降水量（单位：毫米）")

# 设置输入栏（根据你的数据字段）
T = st.slider("气温 T（℃）", -30.0, 50.0, 20.0)
Po = st.slider("本站气压 Po（mmHg）", 600.0, 800.0, 750.0)
P = st.slider("海平面气压 P（mmHg）", 600.0, 800.0, 760.0)
Pa = st.slider("气压变化 Pa", -20.0, 20.0, 0.0)
U = st.slider("湿度 U（%）", 0, 100, 60)
DD = st.slider("风向 DD（角度）", 0, 360, 90)
Ff = st.slider("风速 Ff（m/s）", 0.0, 30.0, 3.0)
ff3 = st.slider("最大阵风 ff3（m/s）", 0.0, 40.0, 5.0)
N = st.slider("总云量 N（0-10）", 0, 10, 5)
Tn = st.slider("最低气温 Tn（℃）", -30.0, 50.0, 15.0)
Tx = st.slider("最高气温 Tx（℃）", -30.0, 50.0, 25.0)
H = st.slider("最低云高 H（m）", 0, 5000, 800)
VV = st.slider("能见度 VV（m）", 0, 10000, 8000)
Td = st.slider("露点温度 Td（℃）", -30.0, 50.0, 10.0)
tR = st.slider("达到规定降雨量时间 tR", 0.0, 30.0, 5.0)
# dd_sin
# dd_cos
year = st.slider("年year", 2021,2035,2025)
month = st.slider("月month",1,12,5)
day = st.slider("日day",1,31,1)
hour = st.slider("时hour",0,24,12)
# hour_sin
# hour_cos
# timestamp
# 添加你其它的输入特征





# 创建输入 DataFrame（确保列顺序与训练时一致）
X_input = pd.DataFrame([[T, Po, P, Pa, U, DD, Ff, ff3, N, Tn, Tx, H, VV, Td, tR,year,month,day,hour]],
                       columns=['t','po','p','pa','u','dd','ff','ff3','n','tn','tx','h','vv','td','tr','year','month','day','hour'])

# dd_sin
# dd_cos
# year
# month
# day
# hour
# hour_sin
# hour_cos
# timestamp

# 预测
if st.button("预测降水量"):
    pred = model.predict(X_input)[0]
    if pred < 0:
        pred = 0
    st.success(f"预测的3小时降水量为：{pred:.2f} mm")
